import React from 'react';
import {inject, observer} from 'mobx-react';
import {WeaReportGroup, WeaRightMenu, WeaTab,WeaTop,WeaDialog,WeaLocaleProvider} from 'ecCom';
import {routerShape} from 'react-router';
import {Button, message} from 'antd';
import PublicTable from './PublicTable';
import ExportInfo from './ExportInfo';
const getLabel = WeaLocaleProvider.getLabel;

@inject('wfViewReportStore')

@observer

export default class Main extends React.Component{

    static contextTypes = {
        router: routerShape
    };

    constructor (props) {
        super(props);
    }

    componentDidMount () {
        let params = this.props.location.query || '';
        const {wfViewReportStore} = this.props;
        wfViewReportStore.doViewSearch(params);
    }

    componentWillUnmount () {
        const {wfViewReportStore} = this.props;
        wfViewReportStore.clearState();
    }

    doReSearch() {
        this.context.router.push({
            pathname : "/main/workflow/viewCustomReport",
            query : {
                reportid : this.props.location.query.reportid,
                tabKey : this.props.location.query.tabKey || 0
            }
        });
    }

    creatChart() {

        const {wfViewReportStore} = this.props;
        let {tableStore} = wfViewReportStore;
        let {columns = {}} = tableStore;

        if (tableStore.count === 0) {
            message.warning(getLabel(383656,'数据不能生成图表！'));
            return ;
        }

        let formulaFlag = false;


        for(let i = 0; i < columns.length; i++){
            if(columns[i].dataIndex.indexOf('formula') > -1){
                formulaFlag = true;
                break ;
            }
        }

        if(!formulaFlag){
            message.warning(getLabel(383656,'数据不能生成图表！'));
            return ;
        }


        let {...params} = this.props.location.query;

        this.context.router.push({
            pathname : "/main/workflow/customReportChart",
            query : {
                sessionkey : tableStore.dataKey,
                ...params,
            }
        });
    }

    exportExcel() {
        const {wfViewReportStore,location} = this.props;
        let reportid = location.query.reportid;
        wfViewReportStore.exportExcel(true,reportid);
    }

    getButtons () {
        let btns = [];
        btns.push(
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@oj774e@doReSearch`} type="primary" onClick={() => this.doReSearch()}>{getLabel(383659,'重新搜索')}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5aq1n1@creatChart`} type="primary" onClick={() => this.creatChart()}>{getLabel(383664,'生成图表')}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3pgb77@exportExcel`} type="primary" onClick={() => this.exportExcel()}>{getLabel(132190,'导出Excel')}</Button>),
        );
        return btns;
    }

    dropMenuDatas () {

        let btns = [];

        btns.push(
            {
                key: "1",
                icon: <i className="icon-coms-search"/>,
                content: getLabel(383659,'重新搜索'),
                onClick: key => {
                    this.doReSearch();
                }
            },{
                key: "2",
                icon: <i className="icon-coms-chart"/>,
                content: getLabel(383664,'生成图表'),
                onClick: key => {
                    this.creatChart();
                }
            },{
                key: "3",
                icon: <i className="icon-coms-plate"/>,
                content: getLabel(132190,'导出Excel'),
                onClick: key => {
                    this.exportExcel();
                }
            }
        )

        return btns;
    }


    render () {
        const {wfViewReportStore} = this.props;
        let { loading,resultTitle,tableStore,exportExcelUrl = '',exportInfo = []} = wfViewReportStore;

        return (
            <div>
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@ebyhck`} datas={this.dropMenuDatas()}>
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@kl9nyn`}
                        title={resultTitle}
                        loading={loading}
                        icon={<i className="icon-coms-workflow"/>}
                        iconBgcolor='#0079DE'
                        buttons={this.getButtons()}
                        buttonSpace={10}
                        showDropIcon={true}
                        dropMenuDatas={this.dropMenuDatas()}
                    >

                        {
                            tableStore.dataKey.length > 0 &&
                            <PublicTable ecId={`${this && this.props && this.props.ecId || ''}_PublicTable@xwagz9`}
                                tableStore={tableStore}
                            />
                        }

                    </WeaTop>
                    <iframe name="exportExcel" src={exportExcelUrl} style={{display: 'none'}}></iframe>
                    {exportInfo.length > 0 && <ExportInfo ecId={`${this && this.props && this.props.ecId || ''}_ExportInfo@1m9t3i`}/>}
                </WeaRightMenu>
            </div>
        )
    }

    getColumns(columns) {
        const _this = this;
        if(columns && columns.length > 0){
            const {tableStore} = this.props.wfViewReportStore;
            let newColumns = columns.slice();
            newColumns = newColumns.map(column => {
                let newColumn = column;
                let lastdata = {};
                newColumn.render = (text, record, index) => {
                    const {dataIndex = ''} = newColumn;
                    if(dataIndex === 'requestids') {
                        return (<a style={{color:'#4D7AD8'}} onClick={_this.showReqList.bind(_this,text)}>查看</a>);
                    }else if(!dataIndex.startsWith('formula')) {
                        const tempdate  = lastdata[dataIndex];
                        if( index > 0 && tempdate == text) {
                            return "";
                        } else  {
                            lastdata[dataIndex] = text;
                            return  record[`${dataIndex}span`] ;
                        }
                    } else {
                        return record[`${dataIndex}span`];
                    }
                }
            });
        }
    }
}


